<?php
class SSHSession 
{
	var $con ;
	var $ok ;
	var $stream ;
	
	function SSHSession()
	{
		$this->ok = false ;
	}	
	
	function connect($hostname,$username,$password)
	{		
		if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist") ;
		// log in at server1.example.com on port 22
		if(!($this->con = ssh2_connect($hostname, 22))){
			echo "fail: unable to establish connection\n<br><br><br>";
		} else {
		// try to authenticate with username root, password secretpassword
			if(!ssh2_auth_password($this->con, $username, $password)) {
				echo "fail: unable to authenticate\n<br><br><br>";
			} else {
				// allright, we're in!
				//echo "okay: logged in...\n";
				$this->ok = true ;
			}
		}
	}
	
	function exec($cmd)
	{
		if(!($this->stream = ssh2_exec($this->con,$cmd )) ){
			echo "fail: unable to execute command\n";
		} else{
			// collect returning data from command
			stream_set_blocking( $this->stream, true );
			$data = ""; 
		}
	}
	
	function readln()
	{
		$buf = fgets($this->stream,4096) ;
		return $buf ;
	}
	
	function EOF()
	{
		return feof($this->stream) ;
	}
	function readall()
	{
		$buf = "" ;
		while($tmp = fread($this->stream,4096))
			$buf = $buf.$tmp ;
		return $buf ;
	}
}

/*
$i=0;
$c = new SSHSession();
$c->connect("ggcook.no-ip.info","df602","df602");
$c->exec("df -k | grep /dev/hd.");
while (!$c->EOF())
	echo $c->readln() ;
*/
?>